<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title><%=UserUtil.getCurrentMenuItemName() %></title>
    
    
	<script type="text/javascript" src="<c:url value='/scripts/jquery/jquery.bgiframe-2.1.2.js'/>"></script>
    
    <script type="text/javascript">
    <!--     
    $(document).ready(function() {    
        $('#doAddBtn').click(function() {        	       	
            $("#queryResultDiv").fadeTo("slow", 0.1);

            waitingDialog({});
            $.ajax({url : "<c:url value='/ajax/Soez9002.action'/>",
                async : true,
                type : 'POST',
                data : $("#toAddHiddenFrom").serialize(),
                dataType : 'html',
                success : function(data, textStatus, XMLHttpRequest){
                        $("#queryResultDiv").html(data);
                    },
                error : function(XMLHttpRequest, textStatus, errorThrown){
                    $("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
                    $("#ajaxErrorMessageDiv" ).dialog({  
                        title:'系統錯誤' 
                        ,width: 600
                        , resizable: false
                        , modal: true
                        , buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
                    },
                complete: function(XMLHttpRequest, textStatus){
                	closeWaitingDialog();
                    $("#queryResultDiv").fadeTo("slow", 1);
                }
            });
        });                  
    });    
    
    $(document).ready(function() {    
        $('#doQueryBtn').click(function() {
            var rolePk = $('#rolePk').val();
    
            var errorMessage = '';
            if (rolePk == '') errorMessage = '角色名稱不可空白!!';
            
            if (errorMessage != '')
            {
                $("#ajaxErrorMessageDiv").html(errorMessage);
                $("#ajaxErrorMessageDiv" ).dialog({  
                    title:'請修正下列錯誤' 
                    ,width: 300
                    , resizable: false
                    , modal: true
                    , buttons: {
                        Ok: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
                return false;
            }
            
            $('#hidden\\.rolePk').val(rolePk);    
            doQueryAction();
        });                  
    });
    
    function doQueryAction()
    {
        $("#queryResultDiv").fadeTo("slow", 0.1);

        waitingDialog({});
        $.ajax({url : "<c:url value='/ajax/Soez9002.action'/>",
            async : true,
            type : 'POST',
            data : $("#queryHiddenFrom").serialize(),
            dataType : 'html',
            success : function(data, textStatus, XMLHttpRequest){
                    $("#queryResultDiv").html(data);
                },
            error : function(XMLHttpRequest, textStatus, errorThrown){
                $("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
                $("#ajaxErrorMessageDiv" ).dialog({  
                    title:'系統錯誤' 
                    ,width: 600
                    , resizable: false
                    , modal: true
                    , buttons: {
                        Ok: function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
                },
            complete: function(XMLHttpRequest, textStatus){
            	closeWaitingDialog();
                $("#queryResultDiv").fadeTo("slow", 1);
            }
        });
    }       
    
    function doSavedAction()
    {
    	var roleNo = $('#roleNo').val();
    	var roleName = $('#roleName').val();
        var errorMessage = '';
        if (roleNo == '') errorMessage = errorMessage + '角色代碼不可空白!!<br/>';
        if (roleName == '') errorMessage = errorMessage + '角色名稱不可空白!!<br/>';
        
        
        var isDoComfirm = false;
        $('#addHiddenFrom input:checkbox.chk').each(function(){               
            if ( $(this).attr('checked') ){
                isDoComfirm = true;
            }
        });
        
        if (isDoComfirm == false) errorMessage = errorMessage + '至少設定一筆權限功能!!<br/>';
        
        if (errorMessage != '')
        {
            $("#ajaxErrorMessageDiv").html(errorMessage);
            $("#ajaxErrorMessageDiv" ).dialog({  
                title:'請修正下列錯誤' 
                ,width: 300
                , resizable: false
                , modal: true
                , buttons: {
                    Ok: function() {
                        $( this ).dialog( "close" );
                    }
                }
            });
            return false;
        }
        
        
        var roleUsersList = '';
        $('#roleUsersPk option').each( function() {
            roleUsersList = roleUsersList +  $(this).val() + ',';
        });        
        $('#hidden\\.roleUsersPk').val(roleUsersList);        
        
        if(isDoComfirm){
            waitingDialog({});
            
            $.ajax({url : "<c:url value='/ajax/Soez9002.action'/>",
                async : true,
                type : 'POST',
                data : $("#addHiddenFrom").serialize(),
                dataType : 'html',
                success : function(data, textStatus, XMLHttpRequest){
	                	$("#ajaxErrorMessageDiv").html(data);
						$("#ajaxErrorMessageDiv").dialog({
							title : '<%=UserUtil.getCurrentMenuItemName() %>',
							width : 300,
							resizable : false,
							modal : true,
							buttons : {
								Ok : function() {
									
									if (1==$("#doAjaxAddRoleResultCode").val()){
										doReloadRoleOption($("#addRolePkResultCode").val());
										$('#hidden\\.rolePk').val($("#addRolePkResultCode").val());    
							            doQueryAction();
									}
									
									$(this).dialog("close");
								}
							}
						});                       
                    },
                error : function(XMLHttpRequest, textStatus, errorThrown){
                    $("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
                    $("#ajaxErrorMessageDiv" ).dialog({  
                        title:'系統錯誤' 
                        ,width: 600
                        , resizable: false
                        , modal: true
                        , buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
                    },
                complete: function(XMLHttpRequest, textStatus){
                	closeWaitingDialog();
                }
            });    
        }else{
            $("#ajaxErrorMessageDiv").html("請至少設定一筆權限功能");
                    $("#ajaxErrorMessageDiv" ).dialog({  
                        title:'請修正下列錯誤' 
                        ,width: 300
                        , resizable: false
                        , modal: true
                        , buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
                    return false;
        }
    }
    
    function doModifydAction()
    {
    	var roleName = $('#roleName').val();        
        var errorMessage = '';
        if (roleName == '') errorMessage = errorMessage + '角色名稱不可空白!!<br/>';
        
        
        var isDoComfirm = false;
        $('#confirmHiddenFrom input:checkbox.chk').each(function(){               
            if ( $(this).attr('checked') ){
                isDoComfirm = true;
            }
        });
        
        if (isDoComfirm == false) errorMessage = errorMessage + '至少設定一筆權限功能!!<br/>';
        
        if (errorMessage != '')
        {
            $("#ajaxErrorMessageDiv").html(errorMessage);
            $("#ajaxErrorMessageDiv" ).dialog({  
                title:'請修正下列錯誤' 
                ,width: 300
                , resizable: false
                , modal: true
                , buttons: {
                    Ok: function() {
                        $( this ).dialog( "close" );
                    }
                }
            });
            return false;
        }
        
        
        
        var roleUsersList = '';
        $('#roleUsersPk option').each( function() {
        	roleUsersList = roleUsersList +  $(this).val() + ',';
        });        
        $('#hidden\\.roleUsersPk').val(roleUsersList); 
        
        if(isDoComfirm){
            waitingDialog({});
            
            $.ajax({url : "<c:url value='/ajax/Soez9002.action'/>",
                async : true,
                type : 'POST',
                data : $("#confirmHiddenFrom").serialize(),
                dataType : 'html',
                success : function(data, textStatus, XMLHttpRequest){
                	$("#ajaxErrorMessageDiv").html(data);
					$("#ajaxErrorMessageDiv").dialog({
						title : '<%=UserUtil.getCurrentMenuItemName() %>',
						width : 300,
						resizable : false,
						modal : true,
						buttons : {
							Ok : function() {
								
								if (1==$("#doAjaxModifyRoleResultCode").val()){
									doReloadRoleOption($("#modifyRolePkResultCode").val());
								}
								
								$(this).dialog("close");
							}
						}
					});
                    },
                error : function(XMLHttpRequest, textStatus, errorThrown){
                    $("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
                    $("#ajaxErrorMessageDiv" ).dialog( "destroy" );
                    $("#ajaxErrorMessageDiv" ).dialog({  
                        title:'系統錯誤' 
                        ,width: 600
                        , resizable: false
                        , modal: true
                        , buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
                    },
                complete: function(XMLHttpRequest, textStatus){
                	closeWaitingDialog();
                }
            });    
        }else{
            $("#ajaxErrorMessageDiv").html("請至少設定一筆權限功能");
                    $("#ajaxErrorMessageDiv" ).dialog( "destroy" );
                    $("#ajaxErrorMessageDiv" ).dialog({  
                        title:'請修正下列錯誤' 
                        ,width: 300
                        , resizable: false
                        , modal: true
                        , buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
                    return false;
        }
    }    
    
    function doCheckAll(){
        $('#confirmHiddenFrom input:checkbox.chk').each(function(){              
        $(this).attr("checked", true);
       });
    }
    
    function undoCheckAll(){
        $('#confirmHiddenFrom input:checkbox.chk').each(function(){               
        $(this).attr("checked", false);
       });
    }  
    
    function doAddCheckAll(){
        $('#addHiddenFrom input:checkbox.chk').each(function(){              
        $(this).attr("checked", true);
       });
    }
    
    function undoAddCheckAll(){
        $('#addHiddenFrom input:checkbox.chk').each(function(){               
        $(this).attr("checked", false);
       });
    }
    
    function doReloadRoleOption(rolePk)
	{
		waitingDialog({});
		$.ajax({url : "<c:url value='/ajax/Soez9002.action'/>",
			async : true,
			type : 'POST',
			data : {doReloadRolesOptions : 'doReloadRolesOptions'},
			dataType : 'html',
			success : function(data, textStatus, XMLHttpRequest){
				$('#rolePk').find('option').remove().end().append(data);
				$('#rolePk').val(rolePk);
			
			},
			error : function(XMLHttpRequest, textStatus, errorThrown){
				$("#ajaxErrorMessageDiv").html('系統發生錯誤，請聯絡管理員');
				$("#ajaxErrorMessageDiv" ).dialog({  
					title:'系統錯誤' 
					,width: 600
					, resizable: false
					, modal: true
					, buttons: {
						Ok: function() {
							$( this ).dialog( "close" );
						}
					}
				});
				},
			complete: function(XMLHttpRequest, textStatus){closeWaitingDialog();}
		});
		
	}
    //-->
    </script>
</head>
<body>
<div id="mainAreaCT">
		<jsp:include page="/includes/path.jsp"/>
		<jsp:include page="/includes/subMenu.jsp"/>
		
    <div id="roleFormDiv">
        <stripes:form id="roleFrom" beanclass="com.feib.soeasy.action.Soez9002ActionBean" method="post">
	             
	        <table id="queryForm">
	            <tr>
	                <th>角色名稱<span id="mustKeyInStar">**</span></th>
	                <td>
	                    <stripes:select id="rolePk" name="rolePk" >
	                        <stripes:option value="">-- 請選擇 --</stripes:option>
	                        <stripes:options-collection collection="${actionBean.rolesOptions }" label="label" value="value"/>
	                    </stripes:select>
	                </td>
	            </tr>
	            <tr>
	                <td colspan="2" class="queryFunction">
	                    <stripes:button id="doQueryBtn" name="doQueryBtn" value="查詢" class="formBtn"/>
	                    <stripes:button id="doAddBtn" name="doAddBtn" value="新增" class="formBtn"/>
	                </td>
	            </tr>
	        </table>
        </stripes:form>
    </div>
    
    <!-- 角色查詢結果畫面  -->
    <div id="queryResultDiv" style="display: none;">
    </div>       
    <div id="queryHiddenFormDiv" style="display: none;">
        <stripes:form id="queryHiddenFrom" beanclass="com.feib.soeasy.action.Soez9002ActionBean" 
            method="post" onsubmit="javascript: return false;">
            <stripes:hidden id="hidden.rolePk" name="rolePk"/>
            
            <input type="hidden" id="method1" name="doAjaxQuery"/>
        </stripes:form>
    </div>    
    
     <!-- 新增角色畫面  -->
    <div id="addResultDiv" style="display: none;">
    </div>       
    <div id="addHiddenFormDiv" style="display: none;">
        <stripes:form id="toAddHiddenFrom" beanclass="com.feib.soeasy.action.Soez9002ActionBean" 
            method="post" onsubmit="javascript: return false;">
            
            <input type="hidden" id="method2" name="doAjaxAdd"/>
        </stripes:form>
    </div> 
    
    <!-- Message -->
    <div id="ajaxErrorMessageDiv" style="display: none;">
    </div>
</div>    
</body>
</html>